//
//  UWCellCol.m
//  Urban Words
//
//  Created by Tim Brandin on 2011-11-29.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "UWCellCol.h"
#import "UWTableViewCell.h"
#import "UWGamesController.h"
#import "UWGameView.h"

@implementation UWCellCol
@synthesize image;
@synthesize name;
@synthesize subtext;
@synthesize borderShade;
@synthesize game;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setImage:nil];
    [self setName:nil];
    [self setBorderShade:nil];
    [self setSubtext:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    touchCancelled = NO;
    [self performSelector:@selector(open) withObject:nil afterDelay:0.03];
    [super touchesBegan:touches withEvent:event];
    [((UWGamesController *)((UWTableViewCell *)[self.view superview]).parent) 
            setScrollDelegate:self];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    image.alpha = 1;
    touchCancelled = YES;
    
    if (gameview == nil) {
        gameview = [[UWGameView alloc] initWithNibName:@"UWGameView" bundle:nil];
    }
    
    [gameview setGame:game];
    
    gameview.hidesBottomBarWhenPushed = YES;
    
    // push game view controller to navigation stack.
    [[((UWGamesController *)((UWTableViewCell *)[self.view superview]).parent) 
     navigationController] pushViewController:gameview animated:YES];
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchCancelled = YES;
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchCancelled = YES;
    [super touchesMoved:touches withEvent:event];
}

- (void)open
{
    if (!touchCancelled) {
        image.alpha = 0.5;
    }
}

- (void)dealloc {
    [image release];
    [name release];
    [borderShade release];
    [subtext release];
    [super dealloc];
}

- (void)stopScroll {
    image.alpha = 1;
}


@end
